/* tests

- This dofile performs a balance test on the treatment groups

- This is also a dofile to compile test results to a school average that can be merged
with all other datasets.

*/


set more off

********************************************************************************
***************************** 3rd GRADE PORTUGUESE *****************************
********************************************************************************

use "$Baseline_dtRaw/tests/01-261115_MR=Portugues_3CLASSE_F.dta", clear

keep ID_Escola ID_TESTE classificacao_total

rename (ID_Escola ID_TESTE classificacao_total) (school_id test_id score)

gen municipality = .
replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,40,45,61,86,110)
replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,77,78,79,80,81)
replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)
replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)


gen treatment = .
replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33,39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)
replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51,56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)
replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41,48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)
replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43,44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

order municipality treatment, after(school_id)

lab var school_id	"School ID"
lab var test_id		"Test ID"
lab var score		"Student score"
lab var municipality "Municipality"
lab var treatment "Treatment group"

#delimit ;
;
label define treatment
0	"Control group"
1	"Door-to-door"
2	"Meetings"
3	"Both treatments"
;
label define municipality
1	"Sumbe"
2	"Porto Amboim"
3	"Gabela"
4	"Conda"
5	"Ebo"
6	"Quilenda"
7	"Seles"
8	"Cassongue"
9	"Quibala"
;
#delimit cr

lab val municipality municipality
lab val treatment treatment

save "$Baseline_dtRaw/_intermediate files/2015POR3", replace

sort school_id
by school_id: egen test15_score_school_pt3 = mean(score)
keep school_id test15_score_school_pt3
duplicates drop
save "$Baseline_dtRaw/_intermediate files/2015POR3_school", replace

********************************************************************************
**************************** 3rd GRADE MATHEMATICS *****************************
********************************************************************************


use "$Baseline_dtRaw/tests/02-261115_MR=2_Matematica_3CLASSE_F.dta", clear

keep ID_ESCOLA ID_TESTE classificacao_total

rename (ID_ESCOLA ID_TESTE classificacao_total) (school_id test_id score)

gen municipality = .
replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,40,45,61,86,110)
replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,77,78,79,80,81)
replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)
replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)


gen treatment = .
replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33,39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)
replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51,56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)
replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41,48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)
replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43,44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

order municipality treatment, after(school_id)

lab var school_id	"School ID"
lab var test_id		"Test ID"
lab var score		"Student score"
lab var municipality "Municipality"
lab var treatment "Treatment group"

lab val municipality municipality
lab val treatment treatment

save "$Baseline_dtRaw/_intermediate files/2015MAT3", replace

sort school_id
by school_id: egen test15_score_school_mat3 = mean(score)
keep school_id test15_score_school_mat3
duplicates drop
save "$Baseline_dtRaw/_intermediate files/2015MAT3_school", replace

********************************************************************************
***************************** 4th GRADE PORTUGUESE *****************************
********************************************************************************

use "$Baseline_dtRaw/tests/03-261115_MR=Portugues_4CLASSE_F.dta", clear


keep ID_ESC ID_TESTE classificacao_total

rename (ID_ESC ID_TESTE classificacao_total) (school_id test_id score)

gen municipality = .
replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,40,45,61,86,110)
replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,77,78,79,80,81)
replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)
replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)


gen treatment = .
replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33,39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)
replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51,56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)
replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41,48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)
replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43,44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

order municipality treatment, after(school_id)

lab var school_id	"School ID"
lab var test_id		"Test ID"
lab var score		"Student score"
lab var municipality "Municipality"
lab var treatment "Treatment group"

lab val municipality municipality
lab val treatment treatment

save "$Baseline_dtRaw/_intermediate files/2015POR4", replace

sort school_id
by school_id: egen test15_score_school_pt4 = mean(score)
keep school_id test15_score_school_pt4
duplicates drop
save "$Baseline_dtRaw/_intermediate files/2015POR4_school", replace

********************************************************************************
**************************** 4th GRADE MATHEMATICS *****************************
********************************************************************************

use "$Baseline_dtRaw/tests/04-261115_MR=Matematica_4CLASSE_F.dta", clear

keep ID_Escola ID_TESTE classificacao_total

rename (ID_Escola ID_TESTE classificacao_total) (school_id test_id score)

gen municipality = .
replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,40,45,61,86,110)
replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,77,78,79,80,81)
replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)
replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)


gen treatment = .
replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33,39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)
replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51,56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)
replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41,48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)
replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43,44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

order municipality treatment, after(school_id)

lab var school_id	"School ID"
lab var test_id		"Test ID"
lab var score		"Student score"
lab var municipality "Municipality"
lab var treatment "Treatment group"

lab val municipality municipality
lab val treatment treatment

save "$Baseline_dtRaw/_intermediate files/2015MAT4", replace

sort school_id
by school_id: egen test15_score_school_mat4 = mean(score)
keep school_id test15_score_school_mat4
duplicates drop
save "$Baseline_dtRaw/_intermediate files/2015MAT4_school", replace

********************************************************************************
****************************** MERGE TEST RESULTS ******************************
********************************************************************************

use "$Baseline_dtRaw/_intermediate files/2015POR3_school", clear

mer 1:m school_id using "$Baseline_dtRaw/_intermediate files/2015POR4_school"
drop _merge

mer 1:m school_id using "$Baseline_dtRaw/_intermediate files/2015MAT3_school"
drop _merge

mer 1:m school_id using "$Baseline_dtRaw/_intermediate files/2015MAT4_school"
drop _merge

foreach var in test15_score_school_pt3 test15_score_school_pt4 				///
	test15_score_school_mat3 test15_score_school_mat4{
	replace `var' = `var'/10
	}

save "$Baseline_dtRaw/_intermediate files/baseline_tests.dta", replace
